<?php
/**
 * Created by PhpStorm.
 * User: Dave
 * Date: 2018/4/11
 * Time: 17:57
 */

namespace Biz\model;


use Doraemon\tools\ImgUrl;

class ShopSpecialTempl extends \Doraemon\pockets\datebase\BaseDbModel
{
    const _CONFIG_FILE_ = 'db_shop';

    const _CONFIG_SELECT_ = 'db_shop';

    const _DATABASE_ = 'shop';

    const _TABLE_NAME = 'shop_special_templ';

    const _SUB_TABLE = false;

    const STATUS_PASSED = 1;
    const STATUS_DELECT = 9;

    const SPECIAL_GOODS = 1;//一行一个商品
    const SPECIAL_TWO_GOODS = 2;//一行两个商品
    const SPECIAL_ACTIVITY = 3;//带图片的链接或则优惠券

    public static $statusDesc = [
        self::STATUS_PASSED => '正常',
        self::STATUS_DELECT => '删除',
    ];

    public static function addSpecialTempl($data)
    {
        if (self::insert($data)->rowcount()) {
            return self::id();
        }
        return false;
    }

    public static function updateSpecialTempl($where, $data)
    {
        $ret = self::update($data, $where);
        if ($ret->errorCode() !== '00000') {
            return false;
        }
        return true;
    }

    public static function getSpecialTemplBySpecialId($field = '*', $id)
    {

        return self::select($field, [
            'AND' => [
                'special_id' => $id,
                'status' => self::STATUS_PASSED,
            ],
            'ORDER' => ['sort' => 'DESC']
        ]);
    }


    public static function delTempl($templId)
    {
        if (self::update(['status' => self::STATUS_DELECT], ['AND' => [
            'id' => $templId,
        ]])->rowcount()) {

            return true;
        }
    }

    public static function getTemplListBySpecialId($specialId)
    {
        $where = [
            'AND' => [
                'special_id' => $specialId,
                'status' => self::STATUS_PASSED
            ],
            'ORDER' => ['sort' => 'DESC'],
        ];
        $result = self::select("*", $where);
        $list = [];
        foreach ($result as $info) {
            self::formatInfo($info);
            $list[] = $info;
        }
        return $list;
    }

    private static function formatInfo(&$info)
    {
        foreach ($info as $key => $value) {
            switch ($key) {
                case 'status':
                    $info['status_desc'] = self::$statusDesc[$value];
                    break;
                case 'banner_img':
                    $info['banner_img_url'] = $value ? ImgUrl::getUrl(ImgUrl::APPTYPE_BIZ, $value, ImgUrl::IMAGE_SIZE_ORI_NPHONE) : '';
                    break;
                case 'coupon_id':
                    if ($value) {
                        $info['coupon_ids'] = explode(',', $value);
                    }
                    break;
            }
        }
    }

    public static function getList($where, $select = '*', $limit = [], $order = [])
	{
		$param = [
			'AND' => $where,
		];
		if ($limit) {
			$param['LIMIT'] = $limit;
		}
		if ($order) {
			$param['ORDER'] = $order;
		}
		$list = self::select($select, $param);
		return $list;
	}

	public static function getSpecialInfoByGoodsId($where)
    {
        return self::get('*', $where);
    }

}